package com.seckill.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.seckill.model.userInfo;
import com.seckill.service.userInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("/user")
public class userInfoController {

    @Autowired
    userInfoService userInfoService;

    @RequestMapping("/login")
    public String login(userInfo userInfo, HttpSession session){

        System.out.println(userInfo);

        QueryWrapper<userInfo> wrapper = new QueryWrapper<>();
        wrapper.eq("username",userInfo.getUsername());
        wrapper.eq("password",userInfo.getPassword());

        userInfo= userInfoService.getOne(wrapper);
        if(userInfo==null){
            return "redirect:/login.html";
        }
        session.setAttribute("user",userInfo);

        return "redirect:../Kill/All";

    }
    @RequestMapping("/logOut")
    public String logOut(HttpSession session){

        session.removeAttribute("user");

        return "redirect:/login.html";

    }

    @RequestMapping("/register")
    @ResponseBody
    public String register(userInfo userInfo){

        boolean save = userInfoService.save(userInfo);
        if (save){
            return "<a href='/login.html'>注册成功</a>";
        }else {
            return "<a href='/register.html'>注册失败</a>";
        }

    }
}
